<?php

declare(strict_types=1);

namespace App\Chat\Controller;

use App\Chat\Annotation\ChatAuth;
use App\Chat\Service\TalkSessionService;
use App\CustHub\Request\CustomerServiceRequest;
use App\CustHub\Service\ChCustomerServiceService;
use Hyperf\Context\Context;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller('ch/chat/setting'),ChatAuth]
class SettingController extends ChatBaseController
{

    #[Inject]
    protected ChCustomerServiceService $service;

    #[Inject]
    protected TalkSessionService $talkSessionService;

    #[PostMapping('getRobotList')]
    public function getRobotList(CustomerServiceRequest $request)
    {
        $data = $request->all();
        $data['main_uid'] = Context::get('main_uid');
        $data['cs_id'] = $data['uid'];
        return $this->success($this->service->getRobotListByType($data));
    }

    /**
     * 黑名单列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getContactList')]
    public function getContactList(): ResponseInterface
    {
        $data = $this->request->all();
        $data['main_uid'] = Context::get('main_uid');
        return $this->success($this->service->getContactList($data));
    }

    #[PostMapping('channelHidden')]
    public function channelHidden(): ResponseInterface
    {
        $params = $this->request->inputs(['id']);

        $this->validate($params, [
            'id' => 'required|integer|min:0',
        ]);

        $id = is_array($params['id']) ? $params['id'] : json_decode($params['id'],true);
        $rst = $this->talkSessionService->batchChange($this->uid(),$id,'is_hidden');
        return $rst ? $this->success('操作成功') : $this->error('操作失败');
    }


    #[PostMapping('channelBlocked')]
    public function channelBlocked(): ResponseInterface
    {
        $params = $this->request->inputs(['id']);

        $this->validate($params, [
            'id' => 'required|integer|min:0',
        ]);
        $id = is_array($params['id']) ? $params['id'] : json_decode($params['id'],true);

        $rst = $this->talkSessionService->batchChange($this->uid(),$id,'is_blocked');
        return $rst ? $this->success('操作成功') : $this->error('操作失败');
    }
}